package nolockqueue;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created on 2021-06-23
 */
public class Main {
    public static void main(String[] args) {

        NoLockQueue queue = new NoLockQueue(10);
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    queue.enQueue(random.nextInt());
                }
            });
        }

        //        //判断入队的顺序
        //        System.out.println("----------------");
        //        try { TimeUnit.SECONDS.sleep(2); } catch (Exception e) { e.printStackTrace(); } finally { }
        //        Node p = queue2.head.getNext();
        //        while (p != null){
        //            System.out.println(p.getValue());
        //            p = p.getNext();
        //        }
        //
        //        System.out.println("----------------");


        for (int i = 0; i < 10; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    queue.dequeue();
                }
            });
        }


        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        executorService.shutdown();


    }
}
